<?php
/**
 * 话题分类列表
 * 【2019-01-26】已加缓存
 */
//require_once dirname(dirname(__DIR__))."/common.php";
require_once dirname(__DIR__) . "/login_filter.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\model\Topic;

$uuid = $Auth['uuid'];
//分类ID
$topic_id = $_REQUEST['topic_id'];
if (empty($topic_id)) {
    Response::output(-1, "参数错误1");
}
//分页
$page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
$page = $page > 0 ? $page : 1;
$pageSize = 10;

// 缓存
$redis = SysResource::getRedis();
$cacheKeyList = REDIS_PREFIX . "topic_class_$page" . "_" . $pageSize . rand(1, 5);
$cacheTime = rand(20, 30);

$topicClass = $redis->get($cacheKeyList);
if (!empty($topicClass)) {
    $topicClass = json_decode($topicClass, true);
} else {
    $db = SysResource::getDB(false);
    $Topic = new Topic($db);
    //根据话题id，查询话题分类视频
    $topicClass = $Topic->topic_class($page, $pageSize, $topic_id);
    if (!empty($topicClass)) {
        $redis->set($cacheKeyList, json_encode($topicClass),$cacheTime);
    }
}

foreach ($topicClass as $k => &$v) {
    if ($v['uuid'] == $uuid) {
        $v['is_user'] = 1;
    } else {
        $v['is_user'] = 0;
    }
}
Response::output(0, "查询成功", $topicClass);
